package com.tss.utils;

import java.lang.reflect.Field;

public class BeanUtils {

	public static Object transformBean(Object dest, Object src) {

		if (dest == null || src == null) {
			return null;
		}

		Field[] destFields = dest.getClass().getDeclaredFields();

		for (Field destField : destFields) {

			try {

				Field srcField = src.getClass().getDeclaredField(destField.getName());

				if (srcField != null && srcField.getClass().equals(destField.getClass())) {

					Object t = org.apache.commons.beanutils.BeanUtils.getProperty(src, destField.getName());
					org.apache.commons.beanutils.BeanUtils.setProperty(dest, destField.getName(), t);

				}

			} catch (Exception e) {
				continue;
			}

		}

		return dest;
	}

	public static Object transform(Object dest, Object src) {

		if (dest == null || src == null) {
			return null;
		}

		Field[] destFields = dest.getClass().getDeclaredFields();

		for (Field destField : destFields) {

			try {
				Object t = org.apache.commons.beanutils.BeanUtils.getProperty(src, destField.getName());
				if (null != t) {
					org.apache.commons.beanutils.BeanUtils.setProperty(dest, destField.getName(), t);
				}

			} catch (Exception e) {
				continue;
			}

		}

		return dest;
	}
		public static <T> T updateBean(T dest,T src){
		if (dest == null || src == null) {
			return null;
		}
		Field[] destFields = dest.getClass().getDeclaredFields();
		for (Field destField : destFields) {

			try {
				Object t = org.apache.commons.beanutils.BeanUtils.getProperty(src, destField.getName());
				if (null != t) {
					org.apache.commons.beanutils.BeanUtils.setProperty(dest, destField.getName(), t);
				}

			} catch (Exception e) {
				continue;
			}
		}
		return dest;
	}

}